﻿#include "FITKAbsGeoVirtualTopoCombine.h"

namespace Interface {
    FITKGeoEnum::FITKGeometryComType Interface::FITKAbsGeoVirtualTopoCombineFaces::getGeometryCommandType()
    {
        return FITKGeoEnum::FITKGeometryComType::FGTVirtualTopoCombineFaces;
    }


    FITKGeoEnum::FITKGeometryComType Interface::FITKAbsGeoVirtualTopoCombineEdges::getGeometryCommandType()
    {
        return FITKGeoEnum::FITKGeometryComType::FGTVirtualTopoCombineEdges;
    }

    void FITKAbsGeoVirtualTopoCombine::removeFromPart(FITKAbsGeoCommand* part)
    {
        if (!part)
        {
            return;
        }

        this->setGeoCommandManager(nullptr);

        // 获取部件拓扑管理器。
        FITKAbsGeoShapeAgent* shapeAgent = part->getShapeAgent();
        if (!shapeAgent)
        {
            return;
        }

        FITKVirtualTopoManager* vMgr = shapeAgent->getVirtualTopoManager();
        if (!vMgr)
        {
            return;
        }

        // 移除创建的拓扑对象。
        for (const VirtualShape & vShape : m_resultTopos)
        {
            FITKShapeVirtualTopoManager* vTopoMgr = vMgr->getShapeVirtualTopoManager(vShape.Type);
            if (!vTopoMgr)
            {
                continue;
            }

            // 拓扑不存在则无需移除。
            FITKAbsVirtualTopo* vTopo = vTopoMgr->getDataByID(vShape.VirtualTopoId);
            if (!vTopo)
            {
                continue;
            }

            if (vTopo->getCombinedTopoCount() == 0)
            {
                continue;
            }

            // 清除合并拓扑关联性。
            vTopo->clearCombinedTopo();

            int nParents = vTopo->getParentTopoCount();
            for (int i = nParents - 1; i >= 0; i--)
            {
                FITKAbsVirtualTopo* vTopoParent = vTopo->getParentTopo(i);
                if (vTopoParent)
                {
                    vTopoParent->removeSubTopo(vTopo);
                }
            }

            int nChildren = vTopo->getSubTopoCount();
            for (int i = nChildren - 1; i >= 0; i--)
            {
                FITKAbsVirtualTopo* vTopoSub = vTopo->getSubTopo(i);
                if (vTopoSub)
                {
                    vTopo->removeSubTopo(vTopo);
                }
            }

            vTopoMgr->removeDataObj(vTopo);
        }
    }
}